package com.changpay.zywy.ui.activity.serivce

import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.utils.DialogUtil.DialogImpl
import com.changpay.zywy.wight.MyEditItemLayout
import kotlinx.android.synthetic.main.activity_self_machine.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class SelfProvidMachineActivity : BaseActivity(), IView {

    private var lastSelectIndex = 0
    private val layoutList = arrayListOf<MyEditItemLayout>()

    override fun attachLayoutRes(): Int {
        return R.layout.activity_self_machine
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "申请自备机入网"
        iv_back.setOnClickListener { finish() }
        // 固定添加一个View
        val myEditItemLayout = MyEditItemLayout(this)
        ll_channel_list.addView(myEditItemLayout)
        layoutList.add(myEditItemLayout)
        initListener()
    }

    override fun start() {

    }

    private fun initListener() {
        /**
         * 动态生成的布局
         */
        view_add.setOnClickListener(View.OnClickListener { view ->
            val myEditItemLayout = MyEditItemLayout(this@SelfProvidMachineActivity)
            layoutList.add(myEditItemLayout)
            lastSelectIndex++
            ll_channel_list.addView(myEditItemLayout)
        })
        view_delete.setOnClickListener(View.OnClickListener {
            if (lastSelectIndex == 0) {
                DialogUtil.showHitDialog(this, "温馨提示", "至少1组SN号", object : DialogImpl {
                    override fun onOk() {
                        super.onOk()
                    }
                })
                return@OnClickListener
            }
            ll_channel_list.removeView(layoutList[lastSelectIndex])
            layoutList.removeAt(lastSelectIndex)
            lastSelectIndex--
        })
        tv_apply.setOnClickListener { takeService() }
    }

    private fun takeService() {
        val etFactoryName = et_factory_name.text.toString()
        if (TextUtils.isEmpty(etFactoryName)) {
            showToast("请输入厂商名称")
            return
        }
        val etModel = et_model.text.toString()
        if (TextUtils.isEmpty(etModel)) {
            showToast("请先输入机具名称")
            return
        }
        val content = StringBuilder()
        for (data in layoutList) {

            if (!TextUtils.isEmpty(data.snNumber)) {
                content.append(data.snNumber).append(",")
            } else {
                showToast("请先SN号为必填项")
                return
            }
        }
        val substring = content.substring(0, content.length - 1)
        val params = HashMap<String, String>()
        params["key"] = token
        params["manufacturer_name"] = etFactoryName
        params["machine_model"] = etModel
        params["data_content"] = substring
        params["client"] = "android"
        RetrofitHelper.service.applicationForSelf(params).sss(this)
        {

        }
    }
}
